import { createWebHistory, createRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { useTokenStore } from "@/stores/token";

const history = createWebHistory();

const router = createRouter({
  history, // 路由模式
  routes: [
    {
      // 登录页面
      path: "/",
      name: "login",
      component: () => import("../views/Login.vue"),
    },
    {
      // 后台主页面
      path: "/home",
      name: "home",
      component: () => import("../components/home/Home.vue"),
      children: [
        {
          // 首页
          path: "/index",
          name: "index",
          component: () => import("../views/Index.vue"),
          meta: {
            title: "首页",
          },
        },
        {
          // 用户管理
          path: "/user",
          name: "user",
          component: () => import("../views/user/User.vue"),
          meta: {
            title: "用户管理",
          },
        },
        {
          // 部门管理
          path: "/department",
          name: "department",
          component: () => import("../views/department/Department.vue"),
          meta: {
            title: "部门管理",
          },
        },
        {
          // 部门管理
          path: "/xiton",
          name: "xiton",
          component: () => import("../components/Layout/Xiton.vue"),
          meta: {
            title: "系统管理",
          },
          children: [
            {
              // 公告管理
              path: "/notice",
              name: "notice",
              component: () => import("../views/notice/Notice.vue"),
              meta: {
                title: "公告管理",
              },
            },
            {
              // 评价管理
              path: "/evaluation",
              name: "evaluation",
              component: () => import("../views/evaluation/Evaluation.vue"),
              meta: {
                title: "评价管理",
              },
            },
            {
              // 反馈管理
              path: "/feedBack",
              name: "feedBack",
              component: () => import("../views/feedBack/FeedBack.vue"),
              meta: {
                title: "反馈管理",
              },
            },
            {
              // 日志管理
              path: "/log",
              name: "log",
              component: () => import("../views/log/Log.vue"),
              meta: {
                title: "日志管理",
              },
            },
          ],
        },
        {
          // 部门管理
          path: "/reservations",
          name: "reservations",
          component: () => import("../components/Layout/Reservations.vue"),
          meta: {
            title: "预约管理",
          },
          children: [
            {
              // 会议管理
              path: "/reservation",
              name: "reservation",
              component: () => import("../views/reservation/Reservation.vue"),
              meta: {
                title: "会议管理",
              },
            },
            {
              // 通知管理
              path: "/message",
              name: "message",
              component: () => import("../views/message/Message.vue"),
              meta: {
                title: "通知管理",
              },
            },
            {
              // 签到管理
              path: "/sign",
              name: "sign",
              component: () => import("../views/sign/Sign.vue"),
              meta: {
                title: "签到管理",
              },
            },
            {
              //会议室管理
              path: "/room",
              name: "room",
              component: () => import("../views/room/Room.vue"),
              meta: {
                title: "会议室管理",
              },
            },
          ],
        },
      ],
    },
    {
      path: "/:pathMatch(.*)*",
      name: "NotFound",
      component: () => import("../views/404/NotFound.vue"),
    },
  ],
});
// GOOD
router.beforeEach((to, from, next) => {
  const tokenStore = useTokenStore();
  if (to.name === "login") {
    next();
  } else if (tokenStore.token) {
    next();
  } else {
    ElMessage({
      showClose: true,
      message: "无权限，请重新登录！",
      type: "error",
    });
    next({
      name: "login",
    });
  }
});
export default router;
